package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.interagriculture.common.Constants;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.Competition;
import com.interagriculture.entity.query.CompetitionQuery;
import com.interagriculture.mapper.CompetitionMapper;
import com.interagriculture.service.CompetitionService;
import com.interagriculture.utils.StringTools;
import com.interagriculture.utils.UploadFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Competition)表服务实现类
 *
 * @author makejava
 * @since 2024-10-24 19:14:15
 */
@Service("competitionService")
@Slf4j
public class CompetitionServiceImpl implements CompetitionService {
    @Resource
    private CompetitionMapper competitionMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Competition queryById(Integer id) {
        return this.competitionMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param competition 实例对象
     * @return 实例对象
     */
    @Override
    public Competition insert(Competition competition) {
        this.competitionMapper.insert(competition);
        return competition;
    }

    /**
     * 修改数据
     *
     * @param competition 实例对象
     * @return 实例对象
     */
    @Override
    public Competition update(Competition competition) {
        this.competitionMapper.update(competition);
        return this.queryById(competition.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        Competition competition = competitionMapper.queryById(id);
        uploadFileUtil.removePicture(Constants.COMPETITION,competition.getId(),competition.getPicture());
        return this.competitionMapper.deleteById(id) > 0;
    }

    /**
     * 查询竞赛信息
     * @param competitionQuery
     * @return
     */
    @Override
    public PageResult findCompetitionInfo(CompetitionQuery competitionQuery) {
        if(competitionQuery!=null && competitionQuery.getPageNum()!=null && competitionQuery.getPageSize()!=null){
            PageHelper.startPage(competitionQuery.getPageNum(),competitionQuery.getPageSize());
        }
        if(competitionQuery!=null && !StringTools.isEmpty(competitionQuery.getOrderBy())){
            PageHelper.orderBy(competitionQuery.getOrderBy());
        }
        List<Competition> competitions = competitionMapper.selectByPage(competitionQuery);
        long count = competitionMapper.count(competitionQuery);

        return new PageResult(count,competitions);
    }

    /**
     * 修改或添加竞赛信息
     * @param competition
     */
    @Override
    @Transactional
    public void saveOrUpdateInfo(Competition competition, MultipartFile picture) {
            if(picture==null){
                competitionMapper.insertOrUpdate(competition);
                return;
            }
            if(competition.getId()==null){
                competitionMapper.insert(competition);
                String searchPath = uploadFileUtil.uploadUserFile(Constants.COMPETITION, competition.getId(), picture);
                competition.setPicture(searchPath);
                competitionMapper.insertOrUpdate(competition);
            }else {
                String searchPath = uploadFileUtil.uploadUserFile(Constants.COMPETITION, competition.getId(), picture);
                competition.setPicture(searchPath);
                competitionMapper.insertOrUpdate(competition);
            }
    }
}
